package com.ghsc.ghxc.jsb.ui.my.activity

import android.os.CountDownTimer
import android.text.TextUtils
import androidx.lifecycle.Observer
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.utils.GsonUtil
import com.cn.appcore.utils.KeyboardUtils
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.config.UserManager
import com.ghsc.ghxc.jsb.databinding.ActivityUpPayPwdBinding

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.my.activity
 * 类名：AboutUsActivity
 * 创建时间：2024/4/25 17:56
 * 创建人 @author： xiaolin
 * 描述 ：Todo...修改支付密码
 */
class UpPayPwdActivity   : BaseVMActivity<UpPayPwdViewModel, ActivityUpPayPwdBinding>() {
    override fun attachLayoutRes(): Int {
        return R.layout.activity_up_pay_pwd
    }


    override fun attachVMClass(): Class<UpPayPwdViewModel>? {
        return UpPayPwdViewModel::class.java
    }

    private var isPwdVisible = false
    private var isPwdVisibleTo = false
    var myCountDownTimer: MyCountDownTimer? = null


    override fun initBar() {
        super.initBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }

    override fun initView() {
        KeyboardUtils.fixAndroidBug5497(this)
        var userInfo = UserManager.getUserInfo()
        userInfo?.let {
            binding.etPhone.setText(it.mobile)
            binding.etPhone.isFocusable = false
        }
    }

    override fun initData() {
        super.initData()
        binding.bar.topTitle.text="修改支付密码"
        binding.bar.topLeftView.setSingleClickListener {
            hideKeyboard(binding.etPhone)
            finish()
        }
        binding.tvBtnCode.setSingleClickListener {
            //验证码
            if (TextUtils.isEmpty(binding.etPhone.text)) {
                showToast("请输入手机号")
                return@setSingleClickListener
            }
            if (binding.etPhone.text.toString().trim().length != 11) {
                showToast("手机格式错误")
                return@setSingleClickListener
            }
            val map: HashMap<String, Any> = HashMap()
//            map["validate"] =it
//            map["captchaid"] = CustKey.CAPTCHA_ID
            map["mobile"] = binding.etPhone.text.toString().trim()
            map["type"]=4
            GsonUtil.mapToJsonStrObjEncryption(map)
                ?.let { it1 -> mViewModel.onPhoneCodeMethod(it1) }
        }
        binding.btnNext.setSingleClickListener {
            //注册
            if (TextUtils.isEmpty(binding.etPhone.text)) {
                showToast("请输入手机号")
                return@setSingleClickListener
            }
            if (TextUtils.isEmpty(binding.etLoginPwd.text)) {
                showToast("请输入新密码")
                return@setSingleClickListener
            }
            if (binding.etLoginPwd.text.toString().trim().length < 6) {
                showToast("密码错误，不得少于6位")
                return@setSingleClickListener
            }
            if (TextUtils.isEmpty(binding.etLoginPwdTo.text)) {
                showToast("请确认新密码")
                return@setSingleClickListener
            }
            if (binding.etLoginPwd.text.toString().trim() != binding.etLoginPwdTo.text.toString()
                    .trim()
            ) {
                showToast("两次密码不一致")
                return@setSingleClickListener
            }
            // 修改密码
            val map: HashMap<String, Any> = HashMap()
            map["mobile"] = binding.etPhone.text.toString().trim()
            map["paypassword"] = binding.etLoginPwd.text.toString().trim()
            map["smscode"] = binding.etCode.text.toString().trim()
            GsonUtil.mapToJsonStrObjEncryption(map)
                ?.let { it1 -> mViewModel.onSetUpPayPwdMethod(it1) }

        }
    }

    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@UpPayPwdActivity) {
                showToast(it)
            }
            againLoginMsg.observe(this@UpPayPwdActivity) {
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()
            }
            onSetUpPayPwdData.observe(this@UpPayPwdActivity, Observer {
                showToast("修改成功")
                finish()
            })
            onPhoneCodeData.observe(this@UpPayPwdActivity) {
                if (myCountDownTimer == null)
                    myCountDownTimer = MyCountDownTimer(60000, 1000)
                showToast("验证码发送成功")
                myCountDownTimer?.start()
            }

        }
    }

    /**
     * 倒计时函数
     */
    inner class MyCountDownTimer(millisInFuture: Long, countDownInterval: Long) :
        CountDownTimer(millisInFuture, countDownInterval) {
        //计时过程
        override fun onTick(l: Long) {
            //防止计时过程中重复点击
            binding.tvBtnCode.isClickable = false
            binding.tvBtnCode.text = (l / 1000).toString() + "s"
        }

        //计时完毕的方法
        override fun onFinish() {
            //设置可点击
            binding.tvBtnCode.text = resources.getString(R.string.time_again_str)
            binding.tvBtnCode.isClickable = true
        }
    }

}